package com.zzptc.lhl.bullet;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
    DanmakuView danmakuView;
    EditText editText;
    LinearLayout lin_send;
    DanmakuContext danmakuContext;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        event();
    }


    private void initView() {
        danmakuView=findViewById(R.id.danmu);
        editText=findViewById(R.id.et_danmu);
        lin_send=findViewById(R.id.lin_send);
        danmakuContext=DanmakuContext.create();
    }
    private void event() {
        /**
         * 准备工作
         */
        danmakuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                danmakuView.start();
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuView.enableDanmakuDrawingCache(true);
        danmakuView.prepare(parser,danmakuContext);
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lin_send.getVisibility()==View.GONE){
                    lin_send.setVisibility(View.VISIBLE);

                }else{
                    lin_send.setVisibility(View.GONE);

                }

            }
        });
    }/**
     *创建弹幕解析器
     */
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
                @Override
                protected IDanmakus parse() {
                    return new Danmakus();
                }
            };

    public void send(View view) {
        String sendMessage =editText.getText().toString().trim();
        if (sendMessage.equals("")){
            Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();

        }else{
            sendDaremu(sendMessage);
        }
    }

    private void sendDaremu(String sendMessage) {
        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text=sendMessage;
        danmaku.textSize=40;
        danmaku.textColor= Color.RED;
        danmaku.borderColor=Color.BLUE;
        danmaku.setTime(danmakuView.getCurrentTime());

        danmakuView.addDanmaku(danmaku);
    }
}
